package com.ruoyi.quality.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quality.domain.TQualityFileItem;
import com.ruoyi.quality.service.ITQualityFileItemService;
import com.ruoyi.safe.domain.BizSafeDocAttach;
import com.ruoyi.safe.domain.BizSafeInfor;
import com.ruoyi.safe.domain.vo.BizSafeDocAttachVO;
import com.ruoyi.safe.service.IBizSafeDocAttachService;
import com.ruoyi.system.domain.SysUserPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.quality.mapper.TQualityFileMapper;
import com.ruoyi.quality.domain.TQualityFile;
import com.ruoyi.quality.service.ITQualityFileService;

/**
 * 质量问题文件Service业务层处理
 * 
 * @author mahl2
 * @date 2024-02-05
 */
@Service
public class TQualityFileServiceImpl implements ITQualityFileService 
{
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private TQualityFileMapper tQualityFileMapper;

    @Autowired
    IBizSafeDocAttachService fileDocAttachService;

    @Autowired
    private ITQualityFileItemService iTQualityFileItemService;

    /**
     * 查询质量问题文件
     *
     * @param id 质量问题文件主键
     * @return 质量问题文件
     */
    @Override
    public TQualityFile selectTQualityFileById(Long id)
    {
        TQualityFile tQualityFile = tQualityFileMapper.selectTQualityFileById(id);
        tQualityFile.setBucket(bucketName);
        /** 资料文档附件列表 */
        List<BizSafeDocAttachVO> fileDocAttachList = getFileDocAttachList(tQualityFile.getFileInfo());
        tQualityFile.setFileDocAttachList(fileDocAttachList);
        return tQualityFile;
    }
    public List<BizSafeDocAttachVO> getFileDocAttachList(String ids)
    {
        BizSafeDocAttach fileDocAttach = new BizSafeDocAttach();
        String[] strlist = ids.split(",");
        List<Long> listIds = new ArrayList<Long>(strlist.length);
        for (int i = 0; i < strlist.length; ++i)
        {
            listIds.add( Long.parseLong(strlist[i]));
        }
        fileDocAttach.setIds(listIds);
        /** 资料文档附件列表 */
        List<BizSafeDocAttachVO> fileDocAttachList = fileDocAttachService.selectFileDocAttachListIds(fileDocAttach);
        return fileDocAttachList;
    }

    /**
     * 查询质量问题文件列表
     * 
     * @param tQualityFile 质量问题文件
     * @return 质量问题文件
     */
    @Override
    public List<TQualityFile> selectTQualityFileList(TQualityFile tQualityFile)
    {
        List<TQualityFile> list = tQualityFileMapper.selectTQualityFileList(tQualityFile);
        for (TQualityFile item : list)
        {
            // 超期计算
            if (item.getFileRectificationTime() == null)
            {
                item.setFileBeOverdueStatus("0");
            }
            else
            {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
                Date last = null; // 进行转换
                try {
                    last = sdf.parse(item.getFileRectificationTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                last.setHours(23);
                last.setMinutes(59);
                last.setSeconds(59);
                int v = last.compareTo(DateUtils.getNowDate());
                item.setFileBeOverdueStatus(v >= 0 ? "0" : "1");
            }

            item.setBucket(bucketName);
        }
        return list;
    }

    /**
     * 新增质量问题文件
     * 
     * @param tQualityFile 质量问题文件
     * @return 结果
     */
    @Override
    public int insertTQualityFile(TQualityFile tQualityFile)
    {
        tQualityFile.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFile.setCreateBy(loginUser.getUserId().toString());
        return tQualityFileMapper.insertTQualityFile(tQualityFile);
    }

    /**
     * 拆分质量问题文件
     *
     * @param listFileItems 质量问题拆分文件
     * @return 结果
     */
    @Override
    public Integer splitFileNew(List<TQualityFileItem> listFileItems,String fileId)
    {
        TQualityFileItem tQualityFileItem = new TQualityFileItem();
        tQualityFileItem.setFileId(Long.parseLong(fileId));
        for (TQualityFileItem item: listFileItems)
        {
            iTQualityFileItemService.insertTQualityFileItem(item);
        }
        return listFileItems.size()==0?1:listFileItems.size();
    }

    /**
     * 拆分质量问题文件
     *
     * @param listFileItems 质量问题拆分文件
     * @return 结果
     */
    public int splitFile(List<TQualityFileItem> listFileItems,String fileId)
    {
        Long[] idsTemp = new Long[listFileItems.size()];
        int nDelNum = 0;
        TQualityFileItem tQualityFileItem = new TQualityFileItem();
        tQualityFileItem.setFileId(Long.parseLong(fileId));
        List<TQualityFileItem> listFileItemExists = iTQualityFileItemService.selectTQualityFileItemList(tQualityFileItem);
        if (listFileItems.size() > 0)
        {
            /** 质量文件id */

            for (TQualityFileItem itemExist: listFileItemExists)
            {
                boolean bExist = false;

                for (TQualityFileItem item: listFileItems)
                {
                    if (item.getId() == null)
                    {
                        continue;
                    }
                    else
                    {
                        if (item.getId().equals(itemExist.getId()))
                        {
                            bExist = true;
                            break;
                        }
                    }
                }
                if (!bExist)
                {
                    idsTemp[nDelNum] = itemExist.getId();
                    ++nDelNum;
                }
            }

            if (nDelNum > 0)
            {
                Long[] ids = new Long[nDelNum];
                for (int i = 0; i < nDelNum; ++i)
                {
                    ids[i] = idsTemp[i];
                }
                iTQualityFileItemService.deleteTQualityFileItemByIds(ids);
            }
            for (TQualityFileItem item: listFileItems)
            {
                if (item.getId() == null)
                {
                    iTQualityFileItemService.insertTQualityFileItem(item);
                }
                else
                {
                    iTQualityFileItemService.updateTQualityFileItem(item);
                }
            }

        }else{
            Long[] ids = new Long[listFileItemExists.size()];
            for(int i=0; i<listFileItemExists.size();i++){
                ids[i] = listFileItemExists.get(i).getId();
            }
            iTQualityFileItemService.deleteTQualityFileItemByIds(ids);
        }
        return listFileItems.size()==0?1:listFileItems.size();
    }

    /**
     * 修改质量问题文件
     * 
     * @param tQualityFile 质量问题文件
     * @return 结果
     */
    @Override
    public int updateTQualityFile(TQualityFile tQualityFile)
    {
        tQualityFile.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tQualityFile.setUpdateBy(loginUser.getUserId().toString());
        return tQualityFileMapper.updateTQualityFile(tQualityFile);
    }

    /**
     * 批量删除质量问题文件
     * 
     * @param ids 需要删除的质量问题文件主键
     * @return 结果
     */
    @Override
    public int deleteTQualityFileByIds(Long[] ids)
    {
        return tQualityFileMapper.deleteTQualityFileByIds(ids);
    }

    /**
     * 删除质量问题文件信息
     * 
     * @param id 质量问题文件主键
     * @return 结果
     */
    @Override
    public int deleteTQualityFileById(Long id)
    {
        return tQualityFileMapper.deleteTQualityFileById(id);
    }
}
